<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>
      <strong v-if="isRoot"><i class="el-icon-s-home" />{{ treeDools.name }}</strong>
      <span v-else><i class="el-icon-user-solid" />{{ treeDools.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeDools.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addDept">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="editDepartment">查看/编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delDepartment">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/departments'
export default {
  props: {
    treeDools: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      required: true
    }
  },
  methods: {
    async delDepartment() {
      try {
        // 1. 二次确认
        await this.$confirm('是否确认删除该部门')
        // 2. 发起请求
        console.log(1)
        await delDepartment(this.treeDools.id)
        // 3. 提醒用户
        this.$message.success('删除成功')
        // 4. 刷新页面
        this.$emit('delDepartment') // 父传子
      } catch (error) {
        console.log(error)
      }
    },
    // 添加子部门
    addDept() {
      this.$emit('addDept', this.treeDools)
    },
    // 查看/编辑部门
    editDepartment() {
      this.$emit('addDept', this.treeDools)
    }
  }
}
</script>

<style>
</style>
